package com.org.redis.utils;

import java.util.Random;

public class CodeUtil {

    static final char[] DIGITS = {'0','1','2','3','4','5','6','7','8','9',
            'a','b','c','d','e','f','g','h','i','j',
            'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
            'A','B','C','D','E','F','G','H','I','J',
            'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

    public static String toOtherBaseString(long n, int base){
        long num = 0;
        if(n < 0) {
            num = ((long) 2 * 0x7fffffff) + n + 2;
        }else {
            num = n;
        }
        char[] buf = new char[32];
        int pos = 32;
        while( (num/base) > 0 ){
            buf[--pos] = DIGITS[(int)(num % base)];
            num /= base;
        }
        buf[--pos] = DIGITS[(int)(num % base)];
        return new String(buf,pos,(32-pos));
    }

    public static String randomString(int len){
        StringBuilder sb = new StringBuilder();
        int _int = DIGITS.length;
        Random random = new Random();
        for (int i=0;i<len;i++){
            sb.append(DIGITS[random.nextInt(_int)]);
        }
        return sb.toString();
    }


}
